/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.struts.service.beans;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.struts.service.RequestsConfig;
import opiam.admin.faare.struts.service.StrutsService;
import opiam.admin.faare.struts.service.beans.JBRequest;
import opiam.admin.faare.struts.service.beans.JBRequests;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;


/**
 * Test of the JBRequests.
 *
 */
public class JBRequestsTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(JBRequestsTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Requests configuration file name. */
    public static final String REQUESTS_XML_FILE = "/config/service/struts/requests_conf.xml";

    /** Instance of JBRequests. */
    private JBRequests jbRequestsLoaded = null;

    /**
     * Creates a new JBRequestsTest object.
     *
     * @param arg0  Parameter.
    */
    public JBRequestsTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();
            jbRequestsLoaded = RequestsConfig.getInstance().readConfig(StrutsService.class.getResourceAsStream(
                        REQUESTS_XML_FILE));
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("JBRequestsTest constructor error\n");
            msg.append(e.toString());
            _test.displayResultFailure(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new JBRequestsTest("testBegin"));

        suite.addTest(new JBRequestsTest("testAddRequest"));
        suite.addTest(new JBRequestsTest("testFindRequestByName"));

        suite.addTest(new JBRequestsTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("JBRequestsTest");

        _test.displayHeaderClass("JBRequestsTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("JBRequestsTest");
    }

    /**
     * Tests the addRequest method.
     */
    public void testAddRequest()
    {
        _test.displayHeaderMethod("testAddRequest");

        try
        {
            JBRequest jbRequest = new JBRequest();

            jbRequest.setName("newRequest");

            Map resultRequestsMap = new HashMap(jbRequestsLoaded.getRequestsMap());
            Map expectedRequestsMap = new HashMap(jbRequestsLoaded.getRequestsMap());

            int expectedSize1 = expectedRequestsMap.size();
            expectedRequestsMap.put("newRequest", jbRequest);

            int expectedSize2 = expectedRequestsMap.size();

            int resultSize1 = resultRequestsMap.size();
            jbRequestsLoaded.addRequest(jbRequest);
            resultRequestsMap = jbRequestsLoaded.getRequestsMap();

            int resultSize2 = resultRequestsMap.size();

            if (expectedSize2 == resultSize2)
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testAddRequest");
    }

    /**
     * Test the method: findRequestByName.
     *
     */
    public void testFindRequestByName()
    {
        _test.displayHeaderMethod("testFindRequestByName");

        try
        {
            Map requestsMap = new HashMap();
            requestsMap = jbRequestsLoaded.getRequestsMap();

            JBRequest expectedjbRequest = (JBRequest) requestsMap.get(
                    "nomsearch");
            JBRequest resultjbRequest = jbRequestsLoaded.findRequestByName(
                    "NomSearch");

            if (expectedjbRequest.getName().equals(resultjbRequest.getName()))
            {
                _test.displayResultSuccess(null);
            }
            else
            {
                _test.displayResultFailure(null);
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testFindRequestByName");
    }
}
